package com.fahai.web.controller;

import com.fahai.common.PageVo;
import com.fahai.utils.json.JsonResult;
import com.fahai.web.entity.SceneInfo;
import com.fahai.web.service.SceneInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 场景和充值控制器
 */

@Controller
@RequestMapping("/scene")
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SceneInfoController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(SceneInfoController.class);

    @Autowired
    SceneInfoService service;

    /**
     * 返回界面
     *
     * @param model
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','场景及产品管理-0','场景及产品管理-1')")
    @RequestMapping(value = "/management_list", method = RequestMethod.GET)
    public String goPage(Model model) {
        model.addAttribute("selectTab", "场景及产品管理");
        return "management_list";
    }

    /**
     * 充值产品设定列表
     *
     * @param pageVo
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','场景及产品管理-0','场景及产品管理-1')")
    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public JsonResult query(PageVo pageVo, SceneInfo vo, Model model) {
        PageVo pageVoData = service.findSceneList(vo, pageVo);
        model.addAttribute("selectBar", "场景及产品管理");
        return ok("获取充值产品设定数据", pageVoData);
    }

    /**
     * 充值产品设定总数
     *
     * @param pageVo
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','场景及产品管理-0','场景及产品管理-1')")
    @ResponseBody
    @RequestMapping(value = "/count", method = RequestMethod.POST)
    public JsonResult count(PageVo pageVo, SceneInfo vo) {
        int count = service.count(vo);
        return ok("获取充值产品设定数据", count);
    }


    /**
     * 修改
     *
     * @param sceneInfo
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','场景及产品管理-0')")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult update(SceneInfo sceneInfo) {
        service.update(sceneInfo);
        return ok("修改成功");
    }


    /**
     * 增加市场活动设定
     *
     * @param sceneInfo
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','场景及产品管理-0')")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult add(SceneInfo sceneInfo) {
        sceneInfo.setStats(1);//TODO 待确认
        service.add(sceneInfo);
        return ok("增加成功");
    }

    @PreAuthorize("hasAnyAuthority('ADMIN','场景及产品管理-0','场景及产品管理-1')")
    @RequestMapping(value = "/sceneOptions", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult getSceneOptions() {
        SceneInfo sceneInfo = new SceneInfo();
        PageVo pageVo = service.findSceneOptions(sceneInfo);
        return ok("获取场景信息", pageVo);
    }


}
